<?php 
class ImageController extends AdminController
{
	public function actionIndex()
	{
		// delete multi images
		if (isset($_POST['remove_images'])) {
			$remImgs = $_POST['remove_images'];
			if (!is_array($remImgs))
				break;
			// delete image in remote server
			$apiUrl = Yii::app()->params['imageServer'].'deleteMulti';
			$wc = new WebClient();
			$wc->post($apiUrl, array('ids'=>$remImgs));
		}
		
		// get all remote images
		$params = array_merge(array(
			'limit'=>41,
			'offset'=>0
		), $_GET);
		$apiUrl = Yii::app()->params['imageServer'].'/list?'.http_build_query($params);
		$resp = json_decode(file_get_contents($apiUrl), true);
		$images = array();
		if ($resp['code']==200) {
			$images = $resp['data'];
		}
		
		Yii::app()->clientScript->registerCssFile(Yii::app()->theme->baseUrl.'/css/pager.css');
		$this->render('index',array(
			'images'=>$images,
			'paging'=>$params
		));
	}

	public function actionDelete($id) {
		$apiUrl = Yii::app()->params['imageServer'].'delete?id='.$id;
		$resp = json_decode(file_get_contents($apiUrl), true);
		$images = array();
		if ($resp['code']==200) {
			Yii::app()->user->setFlash('success',Yii::t('app', 'Data has been saved'));
		} else {
			Yii::app()->user->setFlash('error',Yii::t('app', 'Error'));
		}
		$this->redirect(array('index'));
	}
}